﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using NewsPublish.Model.Request;
using NewsPublish.Model.Response;
using NewsPublish.Service;

namespace NewsPublish.Web.Controllers
{
    public class NewsController : Controller
    {
        private NewsService _newsService;
        private CommentService _commentService;

        public NewsController(NewsService newsService, CommentService commentService)
        {
            this._newsService = newsService;
            this._commentService = commentService;
        }

        /// <summary>
        /// 根据新闻类别展示新闻列表
        /// </summary>
        /// <param name="classifyid">新闻类别ID</param>
        /// <returns></returns>
        public IActionResult Classify(int classifyid)
        {
            if (classifyid <= 0)
            {
                Response.Redirect("/Home/Index");
            }

            var classify = _newsService.GetOneNewsClassify(classifyid);
            //类别不存在
            if (classify.code == 0)
            {
                Response.Redirect("/Home/Index");

                //当classifyid不存在时,防止页面报错,初始化页面数据
                ViewData["ClassifyName"] = "首页";
                ViewData["NewsList"] = new ResponseModel();
                ViewData["newCommentNews"] = new ResponseModel();
                ViewData["Title"] = "首页";
            }
            else
            {
                ViewData["ClassifyName"] = classify.data.Name;//新闻类别名称

                var newsList = _newsService.GetNewsList(c => c.NewsClassifyId == classifyid, 6);//根据新闻类别ID获取6条新闻
                ViewData["NewsList"] = newsList;//新闻列表

                var newCommentNews = _newsService.GetNewCommentNewsList(c => c.NewsClassifyId == classifyid, 5);
                ViewData["newCommentNews"] = newCommentNews;//新闻列表

                ViewData["Title"] = classify.data.Name;
            }
            return View(_newsService.GetNewsClassifyList());
        }


        /// <summary>
        /// 新闻详情
        /// </summary>
        /// <param name="newsid">新闻id</param>
        /// <returns></returns>
        public IActionResult Detail(int newsid)
        {
            if (newsid <= 0)
            {
                Response.Redirect("/Home/Index");
            }

            var news = _newsService.GetOneNews(newsid);
            ViewData["Title"] = "新闻详情";

            //新闻不存在
            if (news.code == 0)
            {
                Response.Redirect("/Home/Index");

                //当newsid不存在时,防止页面报错,初始化页面数据
                ViewData["News"] = new ResponseModel();
                ViewData["RecommendNews"] = new ResponseModel();
                ViewData["Comments"] = new ResponseModel();

            }
            else
            {
                ViewData["Title"] = news.data.Title + "-新闻详情";
                ViewData["News"] = news;//新闻详情

                var recommendNews = _newsService.GetRecommendNewsList(newsid, 5);
                ViewData["RecommendNews"] = recommendNews;//推荐热门新闻列表

                var comments = _commentService.GetCommentsList(c => c.NewsId == newsid);
                ViewData["Comments"] = comments;
            }
            return View(_newsService.GetNewsClassifyList());
        }


        /// <summary>
        /// 添加一条新闻评论
        /// </summary>
        /// <param name="comment"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddComment(AddComment comment)
        {
            if (comment.NewsId <= 0)
            {
                return Json(new ResponseModel { code = 0, result = "新闻不存在" });
            }
            else if (string.IsNullOrEmpty(comment.Contents))
            {
                return Json(new ResponseModel { code = 0, result = "评论内容不能为空" });
            }

            return Json(_commentService.AddComment(comment));
        }
    }
}
